//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,//判断小程序的API，回调，参数，组件等是否在当前版本可用。
    isHide: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  // onLoad() {
  //   let that = this
  //   app.getLogin().then(function () {
  //     wx.showLoading({
  //       title: '加载中...',
  //       mask: true
  //     })
  //     that.loadBanner()
  //     that.loadNews()
  //     that.loadNotice()
  //     that.loadZhoukan()
  //     wx.hideLoading()
  //   })
  // },

  onShow() {

    let that = this
    app.getLogin().then(function () {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      that.loadBanner()
      that.loadNews()
      that.loadNotice()
      that.loadZhoukan()
      wx.hideLoading()
    })
  },

  onHide(){
    if(app.globalData.if_auth == 0){
      this.setData({
        isHide:false
      })
    }
  },

  getUserInfo: function (e) {
    // 用户点击允许
    console.log(e)
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        isHide: false
      })
      let { encryptedData, iv, rawData, signature } = e.detail
      let access_token = wx.getStorageSync('access_token')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/auth',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          encryptedData: encryptedData,
          iv: iv,
          rawData: rawData,
          signature: signature,
          access_token: access_token
        },
        success: (res) => {
          console.log(res)
          app.globalData.userInfo = res.data.info
          app.globalData.if_auth = 1
          console.log(app.globalData.userInfo)
        }
      })
    } else {
      console.log('用户点击拒绝授权')
    }
  },

  toSocial() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../social-work/social-work',
      })
    }
  },

  toStudy() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../study/study',
      })
    }
  },

  toWork() {
    console.log(this.data)
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../work/work',
      })
    }
  },

  toNewsDetail(e) {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      let news_id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../news-details/news-details?news_id=' + news_id,
      })
    }
  },

  newsMore() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../more-news/more-news',
      })
    }
  },

  zhoukanMore() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../more-zhoukan/more-zhoukan',
      })
    }
  },

  // 轮播图
  loadBanner() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/bannerlist',
      data: {
        access_token: access_token,
        location: 'shouye'
      },
      success: (res) => {
        console.log(res.data)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        }
        else if (res.data.code == 1) {
          this.setData({
            bannerList: res.data.list
          })
        }
      }
    })
  },

  // 新闻列表
  loadNews() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/newslist',
      data: {
        access_token: access_token,
        location: 'xinwen',
        news_num: 3
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            newsList: res.data.list
          })
        }
      }
    })
  },

  loadNotice() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/noticelist',
      data: {
        access_token: access_token,
        location: 'shouye'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            noticeList: res.data.list
          })
        }
      }
    })
  },

  // 近期周刊
  loadZhoukan() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/newslist',
      data: {
        access_token: access_token,
        location: 'zhoukan',
        news_num: 3
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            zhoukanList: res.data.list
          })
        }
      }
    })
  },

  toNoticeDetail() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../noticeList/noticeList',
      })
    }
  }
})
